<?php

class Program {
  public function __construct(dataBaseConnection $connection) {
    $this->Connection=$connection;
    $this->SCPPath =  $this->GetSCPPath();
  }
  
  public function StartSCP($command, $serviceName) {
    $config = new Config($this->Connection);
    $enableNetwork = $config->GetConfigEntryByName("EnableNetwork");
    $execResult = array();
    $precommand="";
    if(!$enableNetwork && $enableNetwork == "True")
    {
      $remoteDevice = $config->GetConfigEntryByName("NetworkPath");
      $username = str_replace("/", "\\", $config->GetConfigEntryByName("NetworkUser"));
      $password = $config->GetConfigEntryByName("NetworkPassword");
      if($remoteDevice != false && $username != false && $password != false)
        $precommand = "psexec \\$remoteDevice -u \"$username\" -p \"$password\" ";
    }
     
    $return = exec($precommand.'"'.$this->SCPPath . '" "' . $serviceName . '" ' . $command,$execResult);
    
    $result = false;
    if($return == "Success")
      $result = true;
    else
    {
    foreach ($execResult as $value) {
      if(strpos($value, "Success")!==false)
              $result = true;
    }
    }
    return $result;
  }
  
  private function GetSCPPath() {
    $config = new Config($this->Connection);
    return str_replace('/', '\\', $config->GetConfigEntryByName("SCPPath"));
  }

  public $SCPPath;
  public $Connection;
}
